package com.example.supermarket.controller;

import com.alibaba.fastjson.JSONArray;
import com.example.supermarket.entity.Member;
import com.example.supermarket.mapper.MemberMapper;
import com.example.supermarket.service.AdminMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * <p>后台会员信息管理</p>
 * @author Bosen
 * @date 2021/10/11 16:37
 */
@RestController
public class AdminMemberController {

    @Autowired
    AdminMemberService adminMemberService;

    @Autowired
    MemberMapper memberMapper;

    /**
     * <p>会员信息页面</p>
     */
    @RequestMapping("/admin/member")
    public ModelAndView member(ModelAndView modelAndView) {
        // 获取所有会员的信息
        List<Member> members = memberMapper.findAll();
        String membersJson = JSONArray.toJSONString(members);
        modelAndView.addObject("membersJson", membersJson);

        modelAndView.setViewName("admin/member");
        return modelAndView;
    }

    /**
     * <p>增加会员信息</p>
     * @return
     * 0 必填信息不能为空
     * 1 成功
     *-1 失败
     * 2 会员卡不存在
     * 3 会员昵称已存在
     * 4 该会员卡已被使用
     */
    @RequestMapping("/admin/member/add")
    public int addMemberInfo(String name, String password, String cardId) {
        return adminMemberService.addMemberInfo(name, password, cardId);
    }
    
    /**
     * <p>编辑会员信息</p>
     * @return
     * 0 昵称不能为空
     *-1 修改失败
     * 1 修改成功
     * 2 会员卡不存在
     * 3 会员卡已被使用
     */
    @RequestMapping("/admin/member/update")
    public int update(int id, String name, String cardId) {
        return adminMemberService.update(id, name, cardId);
    }

    /**
     * <p>删除</p>
     * @return
     * 1 删除成功
     * 0 删除失败
     */
    @RequestMapping("/admin/member/delete")
    public int delete(int id) {
        return adminMemberService.deleteMemberInfo(id);
    }

}
